import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import request from '@/utils/request';
import type { ArrearsTableItem, RecoveryTableItem } from './types.d';

const moduleBaseUrl = '/api/arrears-recovery/v1.0.0';

// 获取欠费列表
export async function queryArrears(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ArrearsTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: ArrearsTableItem[];
  }> = await request(`${moduleBaseUrl}/arrears`, {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

// 获取欠费列表
export async function queryRecoverys(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: RecoveryTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: RecoveryTableItem[];
  }> = await request(`${moduleBaseUrl}/recoverys`, {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function sendArrearMessage(data: {
  phone: string;
  plateNumber: string;
  arrears: number;
  id: string;
}) {
  const res = await request(`${moduleBaseUrl}/push-message`, {
    method: 'POST',
    data,
  });
  if (res?.code === SUCCESS_CODE) {
    return {
      success: true,
      message: '发送成功',
    };
  }
  return { success: false, message: res?.message || '发送失败' };
}

export async function getTotalArrear(params: { ownerId?: string }) {
  const res: ResponseData<{
    total: number;
  }> = await request(`${moduleBaseUrl}/total-arrears`, {
    method: 'GET',
    params,
  });
  if (res?.code === SUCCESS_CODE) {
    return {
      success: true,
      data: res.data?.total || 0,
    };
  }
  return { success: false, message: res?.message || '获取失败', data: 0 };
}
